﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
namespace HetmansProblem
{
    /// <summary>
    /// Board of game.
    /// </summary>
    class Board
    {
        ObservableCollection<List<string>> boardTableH = new ObservableCollection<List<string>>();//board for hetmans
        public ObservableCollection<List<string>> BoardTableH
        {
            get { return boardTableH; }
            set { boardTableH = value; }
        }

        List<List<string>> boardTablePossMov = new List<List<string>>();//board for possible moves of hetmans
        public List<List<string>> BoardTablePossMov
        {
            get { return boardTablePossMov; }
            set { boardTablePossMov = value; }
        }

        public Board()
        {
            for (int i = 0; i < 8; i++)
            {
                BoardTableH.Add(new List<string>());
                for (int j = 0; j < 8; j++)
                {
                    BoardTableH[i].Add("O");
                }
            }
            for (int i = 0; i < 8; i++)
            {
                BoardTablePossMov.Add(new List<string>());
                for (int j = 0; j < 8; j++)
                {
                    BoardTablePossMov[i].Add("O");
                }
            }
        }


        /// <summary>
        /// Finds the free field-ny hetman dont stand on it, and also his moves
        /// </summary>
        /// <returns>Coordinates of field</returns>
        public Coordinate FindFreeField()
        {
            Coordinate _coo = new Coordinate(-1, -1);
            for (int j = 0; j < BoardTableH.Count; j++)
            {
                for (int i = 0; i < BoardTableH[j].Count; i++)
                {
                    if (BoardTableH[j][i] == "O" && BoardTablePossMov[j][i] == "O")
                    {
                        _coo.x = j;
                        _coo.y = i;
                        return _coo;
                    }
                }
            }
            return _coo;
        }
        /// <summary>
        /// Do a possible move of hetman on temporary board
        /// </summary>
        /// <param name="_xMove">Move in x axis.Possibility:-1,1</param>
        /// <param name="_yMove">Move in y axis.Possibility:-1,1</param>
        /// <param name="_cooHetman">Coordinate of hetman</param>
        /// <param name="_tmpBoard">Temporary board for hetman and possible moves</param>
        public void DoPossibleMove(int _xMove, int _yMove, Coordinate _cooHetman, List<List<string>> _tmpBoard)
        {
            _tmpBoard[_cooHetman.x][_cooHetman.y] = "H";
            int _x = _cooHetman.x + _xMove, _y = _cooHetman.y + _yMove;
            while (_x != -1 && _x != 8 && _y != -1 && _y != 8)
            {
                _tmpBoard[_x][_y] = "P";
                _x += _xMove;
                _y += _yMove;
            };
        }

        /// <summary>
        /// Check that is safe to put hetman at field
        /// </summary>
        /// <param name="_freeField">Field to check</param>
        /// <param name="_tmpBoard">Temporary board for hetman and possible moves</param>
        /// <returns>True or false</returns>
        public bool IsSafe(Coordinate _freeField, List<List<string>> _tmpBoard)
        {
            bool _isSafe = true;
            for (int j = 0; j < BoardTableH.Count; j++)
            {
                for (int i = 0; i < BoardTableH[j].Count; i++)
                {
                    if ((BoardTableH[j][i] == "H" && _tmpBoard[j][i] == "P") ||
                        (BoardTablePossMov[j][i] == "P" && _tmpBoard[j][i] == "H"))
                    {
                        _isSafe = false;
                    }
                }
            }
            return _isSafe;
        }
        /// <summary>
        /// Put hetman at safe field
        /// </summary>
        /// <param name="_freeField">Safe field to put hetman</param>
        public void PutH(Coordinate _freeField)
        {
            BoardTableH[_freeField.x][_freeField.y] = "H";
        }
        /// <summary>
        /// Put on main board possible moves of hetman
        /// </summary>
        /// <param name="_freeField">Field of hetman</param>
        /// <param name="_tmpBoard">Temporary board for hetman and possible moves</param>
        public void PutPossibleMoves(Coordinate _freeField, List<List<string>> _tmpBoard)
        {
            for (int j = 0; j < _tmpBoard.Count; j++)
            {
                for (int i = 0; i < _tmpBoard[j].Count; i++)
                {
                    if ((BoardTablePossMov[j][i] != "P") && _tmpBoard[j][i] == "P")
                    {
                        BoardTablePossMov[j][i] = "P";
                    }
                }
            }
        }
    }
}
